package com.start.workflow.core.load;

import com.start.common.entity.StartBaseUser;
import com.start.common.user.StartUserInfoLoadService;
import com.start.workflow.common.model.StartActBusinessModel;
import com.start.workflow.common.model.StartActConfigModel;
import com.start.workflow.common.std.IStartActCandidateLoad;
import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StartActLastTaskCandidateLoad implements IStartActCandidateLoad<StartBaseUser> {
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;

    /**
     * 类别
     *
     * @return
     */
    @Override
    public String categoryKey() {
        return "system_category";
    }

    /**
     * 类别名称
     *
     * @return
     */
    @Override
    public String categoryName() {
        return "系统";
    }

    /**
     * 候选人类型
     *
     * @return
     */
    @Override
    public String actCandidateType() {
        return "last_node_auth_user";
    }

    /**
     * 候选者类型名称
     *
     * @return
     */
    @Override
    public String actCandidateLabel() {
        return "上一节点审批人";
    }

    @Override
    public String actCandidateDesc() {
        return "当前节点由上一节点审批人审批";
    }

    /**
     * 加载
     *
     * @param curActBus
     * @param config
     * @return
     */
    @Override
    public List<StartBaseUser> load(StartActBusinessModel curActBus, StartActConfigModel config) {
        List<Task> list = taskService.createTaskQuery().processInstanceId(curActBus.getProcInstId()).list();
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<String> taskDefKeys = list.stream().map(TaskInfo::getTaskDefinitionKey).collect(Collectors.toList());
        List<HistoricActivityInstance> activityInstances = historyService.createHistoricActivityInstanceQuery().activityType(BpmnXMLConstants.ELEMENT_TASK_USER).processInstanceId(curActBus.getProcInstId()).finished().orderByHistoricActivityInstanceStartTime().desc().list();
        if (activityInstances.isEmpty()) {
            return new ArrayList<>();
        }
        activityInstances = activityInstances.stream().filter(e -> !taskDefKeys.contains(e.getActivityId())).collect(Collectors.toList());
        if (activityInstances.isEmpty()) {
            return new ArrayList<>();
        }
        HistoricActivityInstance activityInstance = activityInstances.get(0);
        String assignee = activityInstance.getAssignee();
        if (StringUtils.isAllBlank(assignee)) {
            return null;
        }
        String[] split = assignee.split(":");
        StartBaseUser baseUser = StartUserInfoLoadService.getInstance().getUserById(split[0], split[1]);
        if (baseUser == null) {
            return null;
        }
        ArrayList<StartBaseUser> users = new ArrayList<>();
        users.add(baseUser);
        return users;
    }

}
